package com.qczl.pme.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qczl.core.controller.BaseController;
import com.qczl.model.Json;
import com.qczl.pme.service.SignService;
import com.qczl.pme.vo.SignVo;

/**
 * 签到控制器
 * 
 * @author SongR
 *
 */
@Controller
@RequestMapping(value = "/sign")
public class SignController extends BaseController {
	private Logger log = Logger.getLogger(this.getClass());

	@Autowired
	SignService service;

	/**
	 * 根据人员编号获取坐班签到记录
	 * 
	 * @作者:SongR
	 * @版本:v1
	 * @日期:2017年9月20日 下午2:02:56
	 */
	@ResponseBody
	@RequestMapping(value = "/zuoban/v1", method = { RequestMethod.GET, RequestMethod.POST })
	public Json getZuobanV1(String stbno, String personid, int start, int size) {
		Json j = new Json();
		try {
			List<SignVo> vos = service.signZuobanV1(stbno, personid, start, size);
			j.setLength(new Long(vos.size()));
			if (vos.size() > 0) {
				j.setMsg("成功获取坐班签到记录！");
				j.setResult(vos);
				j.setSize(service.countSignZuobanv1(stbno, personid));
			} else {
				j.setMsg("暂无签到记录！");
			}
		} catch (Exception e) {
			j.setState(500);
			j.setMsg("获取坐班签到记录失败！");
			j.setSuccess(false);
			log.error("获取坐班签到记录接口v1异常！", e);
		}
		return j;
	}

	/**
	 * 根据人员编号获取活动签到记录
	 * 
	 * @作者:SongR
	 * @版本:v1
	 * @日期:2017年9月20日 下午2:02:56
	 */
	@ResponseBody
	@RequestMapping(value = "/huodong/v1", method = { RequestMethod.GET, RequestMethod.POST })
	public Json getHuodongV1(String stbno, String personid, int start, int size) {
		Json j = new Json();
		try {
			List<SignVo> vos = service.signHuodongV1(stbno, personid, start, size);
			j.setLength(new Long(vos.size()));
			if (vos.size() > 0) {
				j.setMsg("成功获取活动签到记录！");
				j.setResult(vos);
				j.setSize(service.countSignHuodongv1(stbno, personid));
			} else {
				j.setMsg("暂无签到记录！");
			}
		} catch (Exception e) {
			j.setState(500);
			j.setMsg("获取活动签到记录失败！");
			j.setSuccess(false);
			log.error("获取活动签到记录接口v1异常！", e);
		}
		return j;
	}

	/**
	 * 根据人员编号获取会议签到记录
	 * 
	 * @作者:SongR
	 * @版本:v1
	 * @日期:2017年9月20日 下午2:02:56
	 */
	@ResponseBody
	@RequestMapping(value = "/huiyi/v1", method = { RequestMethod.GET, RequestMethod.POST })
	public Json getHuiyiV1(String stbno, String personid, int start, int size) {
		Json j = new Json();
		try {
			List<SignVo> vos = service.signHuiyiV1(stbno, personid, start, size);
			j.setLength(new Long(vos.size()));
			if (vos.size() > 0) {
				j.setMsg("成功获取会议签到记录！");
				j.setResult(vos);
				j.setSize(service.countSignHuiyiv1(stbno, personid));
			} else {
				j.setMsg("暂无签到记录！");
			}
		} catch (Exception e) {
			j.setState(500);
			j.setMsg("获取会议签到记录失败！");
			j.setSuccess(false);
			log.error("获取会议签到记录接口v1异常！", e);
		}
		return j;
	}

	/**
	 * 视频签到
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/video/{key}/{version}", method = { RequestMethod.GET, RequestMethod.POST })
	public Json video(HttpServletRequest request, HttpServletResponse response, HttpSession session,
			@PathVariable String key, @PathVariable String version) {
		Json j = new Json();
		switch (key) {
		case "zhucun":
			switch (version) {
			case "v1":
				log.info("驻村干部视频签到");
				j = videoZhucunV1(request, response, session);
				break;
			default:
				break;
			}
			break;
		case "huodong":
			switch (version) {
			case "v1":
				log.info("活动视频签到");
				j = videoHuodongV1(request, response, session);
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
		return j;
	}

	/**
	 * 活动签到
	 * 
	 * @作者:SongR
	 * @版本:v1
	 * @日期:2017年9月20日 上午9:59:50
	 */
	private Json videoHuodongV1(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
		Json j = new Json();
		try {
			String phone = request.getParameter("phone");
			String stbno = request.getParameter("stbno");
			SignVo person = service.videoHuodongV1(stbno, phone);
			if (null != person) {
				j.setMsg("签到成功！");
				j.setResult(person);
			} else {
				j.setMsg("签到失败！");
				j.setState(201);
			}
		} catch (Exception e) {
			j.setState(400);
			j.setMsg("签到失败！");
			j.setSuccess(false);
			log.error("活动签到接口v1异常！", e);
		}
		return j;
	}

	/**
	 * 指纹签到
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/finger/{key}/{version}", method = { RequestMethod.GET, RequestMethod.POST })
	public Json finger(HttpServletRequest request, HttpServletResponse response, HttpSession session,
			@PathVariable String key, @PathVariable String version) {
		Json j = new Json();
		switch (key) {
		// 会议签到
		case "metting":
			switch (version) {
			case "v1":
				try {
					// j=service.signMettingV1(request, response, session);
				} catch (Exception e) {
					log.error("会议签到v1异常");
				}
				break;
			default:
				break;
			}
			break;
		// 坐班签到
		case "zuoban":
			switch (version) {
			case "v1":
				// TODO 坐班签到
				break;
			default:
				break;
			}
			break;
		// 驻村干部签到
		case "zhucun":
			switch (version) {
			case "v1":
				// TODO 驻村干部签到
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
		return j;
	}

	/**
	 * 获取签到人员信息
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/get/{key}/{version}", method = { RequestMethod.GET, RequestMethod.POST })
	public Json getSign(HttpServletRequest request, HttpServletResponse response, HttpSession session,
			@PathVariable String key, @PathVariable String version) {
		Json j = new Json();
		switch (key) {
		case "zhucun":
			switch (version) {
			case "v1":
				log.info("驻村干部视频签到人员");
				j = getZhucunSignV1(request, response, session);
				break;
			default:
				break;
			}
			break;
		case "huodong":
			switch (version) {
			case "v1":
				log.info("活动视频签到人员");
				j = getHuodongSignV1(request, response, session);
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
		return j;
	}

	/**
	 * 获取活动签到人员
	 * 
	 * @作者:SongR
	 * @版本:v1
	 * @日期:2017年9月20日 上午10:47:39
	 */
	private Json getHuodongSignV1(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
		Json j = new Json();
		try {
			String start = request.getParameter("start");
			String size = request.getParameter("size");
			String stbno = request.getParameter("stbno");
			List<SignVo> vos = service.getHuodongSignV1(stbno, start, size);
			j.setMsg("成功获取签到人员信息！");
			j.setSize(service.countHuodongSignV1(stbno));
			j.setResult(vos);
		} catch (Exception e) {
			j.setMsg("获取签到人员信息失败！");
			j.setSuccess(false);
			j.setState(401);
			log.error("获取签到人员信息接口异常！", e);
		}
		return j;
	}

	/**
	 * 获取签到人员信息
	 * 
	 * @version v1
	 */
	private Json getZhucunSignV1(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
		Json j = new Json();
		try {
			String start = request.getParameter("start");
			String size = request.getParameter("size");
			String stbno = request.getParameter("stbno");
			List<SignVo> vos = service.getZhucunSignV1(stbno, start, size);
			j.setMsg("成功获取签到人员信息！");
			j.setSize(service.countZhucunSignV1(stbno));
			j.setResult(vos);
		} catch (Exception e) {
			j.setMsg("获取签到人员信息失败！");
			j.setSuccess(false);
			j.setState(401);
			log.error("获取签到人员信息接口异常！", e);
		}
		return j;
	}

	/**
	 * 扫码进行驻村干部视频签到
	 * 
	 * @version v1
	 */
	private Json videoZhucunV1(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
		Json j = new Json();
		try {
			String phone = request.getParameter("phone");
			String stbno = request.getParameter("stbno");
			SignVo person = service.videoZhucunV1(stbno, phone);
			if (null != person) {
				j.setMsg("签到成功！");
				j.setResult(person);
			} else {
				j.setMsg("签到失败！");
				j.setState(201);
			}
		} catch (Exception e) {
			j.setState(400);
			j.setMsg("签到失败！");
			j.setSuccess(false);
			log.error("驻村干部视频签到接口v1异常！", e);
		}
		return j;
	}

}
